<template>
	<view class="container">
		<view class="simpleArea">
			<view class="headPortrait T20M" v-show="!isShowAvatar">
				{{sliceUserName()}}
			</view>
			<u-avatar class="headPortrait T20M" v-show="isShowAvatar" :src="userInfo.avatar"></u-avatar>
			<view class="name">
				<view class="T19S">{{sUserInfo.realName}}</view>
				<view class="subText T14R">{{sUserInfo.username}}</view>
			</view>
		</view>
		<view class="infoArea">
			<view class="information" @click="applyAction">
				<view class="T16R">我的申请</view>
				<u-icon name="arrow-right"></u-icon>
			</view>
			<view class="information" v-if="isApprove" @click="approveAction">
				<view class="T16R">我的审批</view>
				<u-icon name="arrow-right"></u-icon>
			</view>
			<view class="information" @click="lockAction">
				<view class="T16R">锁具绑定</view>
				<u-icon name="arrow-right"></u-icon>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		storage
	} from 'common/utils/storage.js'
	export default {
		components: {},
		data() {
			return {
				userInfo: {}, // 杰能用户信息
				sUserInfo: {} // 智能锁用户信息
			}
		},
		onShow() {
			this.getInfo()
			this.getSmartInfo()
		},
		computed: {
			isShowAvatar() {
				return this.userInfo.avatar && this.userInfo.avatar.length > 0;
			},
			isApprove() {
				var permissions = this.$storage.getStorageDataSync("sPermissions")
				return permissions.includes('*:*:*') || permissions.includes('app:authorize:approve') ?
					true : false
			}
		},
		methods: {
			applyAction() {
				this.$Router.push({
					name: 'applyRecord'
				})
			},
			approveAction() {
				this.$Router.push({
					name: 'approveRecord'
				})
			},
			lockAction() {
				this.$Router.push({
					name: 'lockBind'
				})
			},
			// 获取杰能用户信息
			getInfo() {
				if (storage.getStorageDataSync("userInfo")) {
					this.userInfo = storage.getStorageDataSync("userInfo")
				} else {
					plus.runtime.quit();
				}
			},
			// 获取智能锁用户信息
			getSmartInfo() {
				if (storage.getStorageDataSync("sUserInfo")) {
					this.sUserInfo = storage.getStorageDataSync("sUserInfo")
				} else {
					plus.runtime.quit();
				}
			},
			// 分割用户名称-取后两位
			sliceUserName() {
				if (Object.keys(this.sUserInfo).length == 0) {
					return
				}
				let name = this.sUserInfo.realName
				if (name.length <= 2) {
					return name
				} else {
					let length = name.length
					return name.slice(length - 2)
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 100%;
		min-height: 100vh;
		background-color: #F8F8F8;
		box-sizing: border-box;
	}

	.subText {
		color: $--color-Text-3
	}

	.simpleArea {
		width: 100%;
		height: 196rpx;
		padding: 32rpx 34rpx;
		box-sizing: border-box;
		display: flex;
		flex-flow: row nowrap;
		align-items: center;
		background-color: $--color-bg-0;
		position: relative;
	}

	.name {
		height: 96rpx;
		display: flex;
		flex-flow: column nowrap;
		margin-left: 24rpx;
		justify-content: space-between;
	}

	.updatePassword {
		position: absolute;
		right: 34rpx;
		color: $--color-Blue-2;
	}

	.headPortrait {
		height: 132rpx;
		width: 132rpx;
		background: linear-gradient(180deg, #7598FF 0%, #4168DF 100%);
		border-radius: 100%;
		display: flex;
		flex-flow: row nowrap;
		justify-content: center;
		align-items: center;
		color: $--color-Text-1;
	}

	.infoArea {
		box-sizing: border-box;
		margin-top: 20rpx;
		padding-left: 24rpx;
		background-color: $--color-bg-0;
	}

	.information {
		display: flex;
		flex-flow: row nowrap;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		padding: 28rpx 24rpx 28rpx 0;

		&:nth-child(n) {
			border-bottom: 1rpx solid $--color-Grey-1;
		}

		&:last-child {
			border-bottom: none;
		}
	}

	.button {
		box-sizing: border-box;
		border-radius: 20rpx;
		padding: 28rpx 0;
		margin: 32rpx 32rpx 0;
		text-align: center;
		background-color: $--color-bg-0;
	}

	.loginOut {
		color: $--color-Red-5;
	}

	.checkVersion {
		color: $--color-Blue-4;
	}
</style>